<?php
/**
 * @desc 玄武短信服务
 * @author 陈聪/徐芳珍
 */
namespace Service\Sms\Xuanwu;
defined('THINK_PATH') or exit();
use Service\Sms;

class Xuanwu extends Sms{
    /**
     * @desc 构造函数
     * @param array $options 缓存参数
     * @access public
     */
    public function __construct($options=array()) {
        if(empty($options)) {
            $sms_config = C('SMS_CONFIG');
            $options = isset($sms_config['Xuanwu'])?$sms_config['Xuanwu']:array();
        }
        $this->options = $options;
    }

    /**
     * @desc 短信发送接口
     * @param array $smsInfo 手机号码|内容|时间
     * @return string
     */
    public function sendMessage($smsInfo){
        $rts = array();
        $rts['rt'] = 0;
        $rts['msg'] = '';
//        $rts['return_msg'] = '';    //记录第三方接口返回提示语
        $rts['return_json']= '';    //记录第三方接口返回的结果转json的值

        $requestData = array(
            'Account' => $this->options['Account'],   //用户名
            'Password' => $this->options['Password'], //密码
            'batchName' => $this->options['batchName'], //批次名称
            'Phones' => $smsInfo['mobile'],           //手机号
            'Content' =>  $smsInfo['content'],  //短信内容，一般不超过70个字，超长部分自动分段，总长不超过300个字，必须在最前面或者最后面加上【金稻田】字样
        );
        $wsdl = $this->options['api_send_msg_url'];
        $client = new \SoapClient($wsdl);
        $uuid = $this->guid();
        if($smsInfo['sms_type']==6 && $smsInfo['sms_count']>0){
            $MessageDatat=array();
            for ($i = 0; $i < $smsInfo['sms_count']; $i++){
                   $MessageDatat[$i]=array('Phone'=>$smsInfo['mobile'][$i],
                       'Content'=>$requestData['Content'],
                       'vipFlag'=>'false',
                       'customMsgID'=>'',
                       'customNum'=>'');
                }
        }else{
            $MessageDatat=array('Phone'=>$requestData['Phones'],'Content'=>$requestData['Content'],'vipFlag'=>'false','customMsgID'=>'','customNum'=>'');
        }
        $mtpacktmp=array('uuid'=>$uuid,'batchID'=>$uuid,'batchName'=>$requestData['batchName'],'sendType'=>'0','msgType'=>'1','msgs'=>array('MessageData'=>$MessageDatat),'bizType'=>'','distinctFlag'=>'true','scheduleTime'=>'','deadline'=>'');
        $points = $client->Post(array('account'=>$requestData['Account'],'password'=>$requestData['Password'],'mtpack'=>$mtpacktmp));
        $res_arr = (array) $points->PostResult;
        $res_arr['attributes'] = strip_tags($res_arr['attributes']);
        if ($res_arr['result'] === 0 && substr_count($res_arr['attributes'],'success') > 0) {
            $rts['return_json']= json_encode($res_arr);
            $rts['rt'] = 1;
            $rts['msg'] = '发送成功';
        } else {
            $rts['rt'] = 0;
            $rts['msg'] = $res_arr['message'];
            $rts['return_json']= json_encode($res_arr);
        }
        return $rts;
    }

    /**
     * @desc 获取账号信息
     */
    public function queryAccountBalance(){
        $rts = array();
        $rts['rt'] = 0;
        $rts['msg'] = '';
//        $rts['return_msg'] = '';    //记录第三方接口返回提示语
        $rts['return_json']= '';    //记录第三方接口返回的结果转json的值

        $requestData = array(
            'Account'   => $this->options['Account'],  //用户名
            'Password'  => $this->options['Password'], //密码
        );
        $wsdl = $this->options['api_send_msg_url'];

        $client = new \SoapClient($wsdl);
        $param = array('account'=>$requestData['Account'],'password'=>$requestData['Password']);
        $ret = $client->GetAccountInfo($param);
        $res_arr = (array) $ret->GetAccountInfoResult;
        if (!empty($res_arr['Balance'])) {
            $rts['rt'] = 1;
            $rts['msg'] = '查询成功';
            $rts['balance']=$res_arr['Balance'];
            $rts['return_json']= json_encode($res_arr);
        } else {
            $rts['rt'] = 0;
            $rts['msg'] = '查询失败';
            $rts['return_json'] = json_encode($res_arr);
        }
        return $rts;
    }

    /**
     * @desc 生成唯一的uuid
     */
    public function guid() {
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = substr($charid, 0, 8).$hyphen
                    .substr($charid, 8, 4).$hyphen
                    .substr($charid,12, 4).$hyphen
                    .substr($charid,16, 4).$hyphen
                    .substr($charid,20,12);
            return $uuid;
        }
    }

}